package Controlor.Action;

import Controlor.Server.CpictureServer;
import Controlor.ServerImpl.CpictureServerImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Method;

@WebServlet("/CpictureAction/*")
public class CpictureAction extends BaseAction {
    private CpictureServer cpictureServer;
    public CpictureAction(){
        cpictureServer = new CpictureServerImpl();
    }
    //输出对应id的图片
    public void getFirstCpicurlByCommodityId(HttpServletRequest req, HttpServletResponse resp) {
        try{
            FileInputStream inputStream = new FileInputStream(cpictureServer.getFirstCpicurlByCommodityId(Long.parseLong(req.getQueryString())));
            int i = inputStream.available();
            //byte数组用于存放图片字节数据
            byte[] buff = new byte[i];
            inputStream.read(buff);
            //记得关闭输入流
            inputStream.close();
            //设置发送到客户端的响应内容类型
            resp.setContentType("image/*");
            OutputStream out = resp.getOutputStream();
            out.write(buff);
            //关闭响应输出流
            out.close();
        }
        catch (Exception e){
            e.printStackTrace();
        }

    }
}
